<template>
    <div>
        <el-dialog
            title="编辑"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">
            <el-form :model="balanceForm" :rules="rules" status-icon  ref="balanceForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="增加余额：" prop="balance" >
                    <el-input-number v-model="balanceForm.add" :min="0" :max="999999" label="描述文字"></el-input-number>
                </el-form-item>
                <el-form-item label="减少余额：" prop="balance" >
                    <el-input-number v-model="balanceForm.decrease" :min="0" :max="999999" label="描述文字"></el-input-number>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('balanceForm')">保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { updateBalance } from '@/api/userManagement/userList'
export default {
  data () {
    return {
      balanceForm: {
        add: 0, // 余额增加
        decrease: 0, // 余额减少
        TUserId: ''
      },
      rules: {
        usernbalanceame: [
          { required: false, message: '请输入余额', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      token: ''
    }
  },
  methods: {
    handleClose (done) {
      this.resets()
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    init (type) {
      console.log(type)
      this.dialogVisible = true
      this.balanceForm.TUserId = type.tuserId
      this.token = window.sessionStorage.getItem('token')
      console.log('token:' + this.token)
    },
    submitForm (balanceForm) {
      this.$refs[balanceForm].validate(async valid => {
        if (valid) {
          updateBalance(this.balanceForm).then(res => {
            console.log(res)
            if (res.state === 200) {
              this.$message.success('登录成功')
              this.dialogVisible = false
              this.$emit('closeHandler')
              this.resets()
            } else if (res.state === 408) {
              this.$message.error(res.message)
              window.sessionStorage.clear()
              this.$router.push('/login')
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resets () {
      this.balanceForm = {
        add: 0, // 余额增加
        decrease: 0, // 余额减少
        TUserId: ''
      }
    }
  }
}
</script>

  <style lang="scss" scoped>

  </style>
